package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.whmnx.base.bean.WeixinTeamBean;
import com.whmnx.base.constants.CardConfig;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.base.view.CardItemTouchHelperCallback;
import com.whmnx.base.view.CardLayoutManager;
import com.whmnx.base.view.OnSwipeListener;
import com.whmnx.my.R;
import com.whmnx.my.databinding.FragmentWeixinBinding;
import com.whmnx.my.ui.fragment.MyTeamWeixinFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : huang
 * date   : 2020/11/16 0016 9:35
 * version: 1.0
 * @author Administrator
 */
public class TamaWeiModel extends ToolbarViewModel{
    MyTeamWeixinFragment mFragment;
    FragmentWeixinBinding mBinding;


    private List<String> list = new ArrayList<>();
    private List<String> list2 = new ArrayList<>();
    private List<WeixinTeamBean> weixinTeamBeans = new ArrayList<>();

    public TamaWeiModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(MyTeamWeixinFragment myTeamWeixinFragment, FragmentWeixinBinding binding) {
         this.mFragment = myTeamWeixinFragment;
         this.mBinding = binding;
         setTitleText("团队微信群");
        initView();
        initData2();
    }

    private void initView() {
        mBinding.rvList.setItemAnimator(new DefaultItemAnimator());
        MyAdapter myAdapter =  new MyAdapter();
        mBinding.rvList.setAdapter(myAdapter);
        CardItemTouchHelperCallback cardCallback = new CardItemTouchHelperCallback(mBinding.rvList.getAdapter(), list);
        cardCallback.setOnSwipedListener(new OnSwipeListener<String>() {

            @Override
            public void onSwiping(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {
                MyAdapter.MyViewHolder myHolder = (MyAdapter.MyViewHolder) viewHolder;
                viewHolder.itemView.setAlpha(1 - Math.abs(ratio) * 0.2f);
                if (direction == CardConfig.SWIPING_LEFT) {
                    myHolder.dislikeImageView.setAlpha(Math.abs(ratio));
                } else if (direction == CardConfig.SWIPING_RIGHT) {
                    myHolder.likeImageView.setAlpha(Math.abs(ratio));
                } else {
                    myHolder.dislikeImageView.setAlpha(0f);
                    myHolder.likeImageView.setAlpha(0f);
                }
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, String s, int direction) {
                MyAdapter.MyViewHolder myHolder = (MyAdapter.MyViewHolder) viewHolder;
                viewHolder.itemView.setAlpha(1f);
                myHolder.dislikeImageView.setAlpha(0f);
                myHolder.likeImageView.setAlpha(0f);
            }


            @Override
            public void onSwipedClear() {
                list.clear();
                list2.clear();
                for (WeixinTeamBean weixinTeamBean : weixinTeamBeans) {
                    list.add(weixinTeamBean.getPicture() + "—" + weixinTeamBean.getName());
                  //  list2.add(weixinTeamBean.getName());
                }
                mBinding.rvList.getAdapter().notifyDataSetChanged();

               // initData2();
              /*  mBinding.rvList.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.clear();
                        list2.clear();
                        for (WeixinTeamBean weixinTeamBean : weixinTeamBeans) {
                            list.add(weixinTeamBean.getPicture());
                            list2.add(weixinTeamBean.getName());
                        }


                    }
                }, 3000L);*/
            }
        });
        final ItemTouchHelper touchHelper = new ItemTouchHelper(cardCallback);
        final CardLayoutManager cardLayoutManager = new CardLayoutManager(mBinding.rvList, touchHelper);
        mBinding.rvList.setLayoutManager(cardLayoutManager);
        touchHelper.attachToRecyclerView(mBinding.rvList);
      //
    }

    private void initData2() {
        Map<String, Object> params = new HashMap<>();
        HttpClient.get(mFragment.getActivity(), Constants.WECHATPIC, params,new CallBack<List<WeixinTeamBean>>() {
            @Override
            public void onSuccess(List<WeixinTeamBean> result) {
                weixinTeamBeans.clear();
                weixinTeamBeans.addAll(result);
                list.clear();
             //   list2.clear();
                for (WeixinTeamBean weixinTeamBean : result) {
                    list.add(weixinTeamBean.getPicture() + "—" + weixinTeamBean.getName());
                   // list2.add(weixinTeamBean.getName());
                }

                mBinding.rvList.getAdapter().notifyDataSetChanged();
            }
        });
    }


    private class MyAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weixin, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ImageView avatarImageView = ((MyViewHolder) holder).avatarImageView;
            ImageUtil.setImage(avatarImageView,list.get(position).split("—")[0]);
            TextView name = ((MyViewHolder) holder).tvName;
            name.setText(list.get(position).split("—")[1]);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            ImageView avatarImageView;
            ImageView likeImageView;
            ImageView dislikeImageView;
            TextView tvName;

            MyViewHolder(View itemView) {
                super(itemView);
                avatarImageView = (ImageView) itemView.findViewById(R.id.iv_avatar);
                likeImageView = (ImageView) itemView.findViewById(R.id.iv_like);
                dislikeImageView = (ImageView) itemView.findViewById(R.id.iv_dislike);
                tvName = (TextView) itemView.findViewById(R.id.tv_name);
            }
        }
    }
}
